package com.doit.day02;

import com.alibaba.fastjson.JSON;
import com.doit.day01.MyConfig;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import redis.clients.jedis.Jedis;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 1.每5s输出一次当前来了多少用户(去重)  uv
 * 2.将每条数据添加一个字段来标识，如果这个用户的id是第一次出现，那么就标注1，否则就是0
 */
public class _02_练习之消费者jedis之hll {
    public static void main(String[] args) {
        //创建一个jedis对象
        Jedis jedis = new Jedis("linux01", 6379);

        //搞两个线程，一个线程专门用来收集数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                Properties props = new Properties();
                props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, MyConfig.HOST_AND_PORT);
                props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
                props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
                props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"g01");
                //选配的
                props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
                props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
                //先将数据消费出来
                KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
                consumer.subscribe(Arrays.asList("event-log"));
                while (true){
                    ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
                    for (ConsumerRecord<String, String> consumerRecord : poll) {
                        //value 数据拿出来 ==》 {}==> javabean对象 ==》 获取guid 放到set 集合中
                        String json = consumerRecord.value();
                        EventLog eventLog = JSON.parseObject(json, EventLog.class);
                        int guid = eventLog.getGuid();
                        //把guid放到jedis中，选择bitmap数据结构  ==》 key value
//                        jedis.setbit("event-log-bitmap",guid,true);
                        jedis.pfadd("event-log-hll",guid+"");
                    }
                }
            }
        }).start();

        //还有一个线程专门用来5s返回一次结果
        new Thread(new Runnable() {
            @Override
            public void run() {
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        //去jedis中返回bit数组中1的个数
                        Long uv = jedis.pfcount("event-log-hll");
                        System.out.println("当前时间是："+System.currentTimeMillis()+",uv:"+uv);
                    }
                },5000,5000);
            }
        }).start();

    }
}
